package com.example.administrator.szb.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.AskHD;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.SpannableStringUtil;

import java.util.List;


/**
 * Created by stone on 2018/1/2.
 */

public class HDXQAdapter extends android.widget.BaseAdapter {
    private List<AskHD.DiscussBean> list;
    private Context context;
    private LayoutInflater inflater;
    private int current_uid;
    private int color;
    public HDXQAdapter(Context context, List list,int current_uid) {
        this.context = context;
        this.list = list;
        this.current_uid = current_uid;
        inflater=LayoutInflater.from(context);
        color = context.getResources().getColor(R.color.basecolor);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public AskHD.DiscussBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.item_wd3,null);
            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.textView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final AskHD.DiscussBean askHD = getItem(position);
        SpannableStringUtil st = new SpannableStringUtil(holder.textView);
        if (askHD.getFirstUserId()== SPUtils.getDatasInt(SPUtils.USER_ID)){
            //不能点击
            st.addTextWithColor(""+askHD.getFirstUserName(),color);

        }else {
            st.addTextAll(""+askHD.getFirstUserName(), color, new SpannableStringUtil.ClickListener() {
                @Override
                public void onClick() {
                    if (listener!=null){
                        listener.onItemClick(askHD.getFirstUserId(),askHD.getFirstUserName());
                    }
                }
            });
        }
        if (askHD.getSecondUserId()!=current_uid){
            st.addText("\r回复\r");
            if (askHD.getSecondUserId()==SPUtils.getDatasInt(SPUtils.USER_ID)){
                //不能点击
                st.addTextWithColor(""+askHD.getSecondUserName(),color);
            }else {
                st.addTextAll(""+askHD.getSecondUserName(), color, new SpannableStringUtil.ClickListener() {
                    @Override
                    public void onClick() {
                        if (listener!=null){
                            listener.onItemClick(askHD.getSecondUserId(),askHD.getSecondUserName());
                        }
                    }
                });
            }
        }
        st.addText("："+askHD.getCommentString());
        holder.textView.setText(st.getText());
        return convertView;
    }
    class ViewHolder{
        TextView textView;
    }
    private OnItemClickListener listener=null;
    public void setOnItemClickListener(OnItemClickListener l){
        listener = l;
    }
    public interface OnItemClickListener{
        void onItemClick(int id, String name);
    }
}
